package com.gperp.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gperp.common.ContactsCustomsKeywordsResult;
import com.gperp.common.GPResult;
import com.gperp.common.PortalCondition;
import com.gperp.common.PortalResult;
import com.gperp.mapper.AsKeywordsMapper;
import com.gperp.mapper.other.PortalMapper;
import com.gperp.pojo.AsKeywords;
import com.gperp.pojo.Result;
import com.gperp.service.PortalService;

@Service
public class PortalServiceImpl implements PortalService {

	@Resource
	private PortalMapper portalMapper;

	@Resource
	private AsKeywordsMapper asKeywordsMapper;
	
	@Override
	public Result getlikePortal(int page, int rows, PortalCondition condition) {
		if (condition.getKeywords()!=null &&!condition.getKeywords().equals("")) {
			condition.setKeywords("%"+condition.getKeywords()+"%");
		}
		if (condition.getCustomname()!=null &&!condition.getCustomname().equals("")) {
			condition.setCustomname("%"+condition.getCustomname()+"%");
		}
		// 设置分页信息
		PageHelper.startPage(page, rows);
		// 执行查询。。
		List<PortalResult> list = portalMapper.getlikePortal(condition);
		// 取分页结果。。
		PageInfo<PortalResult> pageInfo = new PageInfo<>(list);
		Result r = new Result();
		r.setRows(pageInfo.getList());
		r.setPageIndex(page);
		r.setTotal(pageInfo.getTotal());		
		return r;
	}
	@Override
	public ContactsCustomsKeywordsResult getContactsCustomsKeywordsResult(
			Long id) {
		/*List list=new ArrayList();
		list.add(portalMapper.getCustomsKeywordsResult(id));
		list.add(portalMapper.getContacts(id));
		ContactsCustomsKeywordsResult cck=new ContactsCustomsKeywordsResult();
		cck.setNumresult(list);
		return cck;*/
		List list=new ArrayList();
		list.add(portalMapper.getCustomsKeywordsResult(id));
		list.add(portalMapper.getContacts(id));
		ContactsCustomsKeywordsResult cck=new ContactsCustomsKeywordsResult();
		cck.setNumresult(list);
		return cck;
	}
	
	@Override
	public GPResult updateportal(Long id,String appusername,String apppassword,String iosdownloadurl,String codeiosurl,String androiddownloadurl,String codeandroidurl) {
		try {
			AsKeywords record=new AsKeywords();
			record.setId(id);
			record.setAppusername(appusername);
			record.setApppassword(apppassword);
			record.setIosdownloadurl(iosdownloadurl);
			record.setCodeiosurl(codeiosurl);
			record.setAndroiddownloadurl(androiddownloadurl);
			record.setCodeiosurl(codeiosurl);
			record.setCodeandroidurl(codeandroidurl);
			asKeywordsMapper.updateByPrimaryKeySelective(record);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();		
	}

}
